//
//  MoreLoanTypeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/30.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol MoreLoanTypeViewDelegate {
    @objc optional func cancelSelect() //取消选择
    @objc optional func selectLoanType() //确定选择
    @objc optional func selectLoanDetails(title:String,text:String) //选择贷款详情
    @objc optional func selectLoanBankType(title: String) //选择贷款银行
}

class MoreLoanTypeView: UIView {
    
    weak var delegate : MoreLoanTypeViewDelegate?
    
    fileprivate let cellId = "moreLoanTypeCell"
    fileprivate let dataArray = [[unlimited,lendersOfTheday,oneToThreeDay,greateThreeDay],[unlimited,lessSixMonth,sixToOneYear,oneToTwoYear,twoToThreeYear,threeToFiveYear,greateFiveYear],[unlimited,smallLoanCompany,warrantyCompany,other,bank_title],[unlimited,installment,equivalentInterest,firstInterest,loanAndThen,disposableDebtService,equivalentPrincipal],[unlimited,operating,consumption,Business,shortTermTurnaround,investment,decoration,tourism,study,buyCar,buyHouse,education]]
    fileprivate let titleArray = [lendingTime,durationofBorrowing,typeofInstitution,repaymentMethod,use]
    
    fileprivate var isFlag = false //重新刷新tableView
    
    fileprivate var heightArray = [CGFloat]() //cell高度
    
    var selectIndex = 0 //选中标签
    
    fileprivate var tableView : UITableView!
    
    fileprivate var moreBankView : MoreLoanBankView! //更多银行

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let footView = MoreTypeFootView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 60)))
        footView.cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        footView.confirmBtn.addTarget(self, action: #selector(selectLoanType), for: .touchUpInside)
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.showsVerticalScrollIndicator = false
        tableView.bounces = false
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(MoreLoanTypeCell.self, forCellReuseIdentifier: cellId)
        tableView.tableFooterView = footView
        self.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
    }
    
    
    //MARK: 取消
    @objc fileprivate func cancelAction() {
        
        self.delegate?.cancelSelect!()
    }
    
    //MARK: 选择贷款详情
    @objc fileprivate func selectLoanType() {
        
        self.delegate?.selectLoanType!()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MoreLoanTypeView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if isFlag {
            if indexPath.row == 2 {
                if self.selectIndex == 4 {//更多银行
                    return heightArray[indexPath.row] + RATIO_H(maxNum: 280)
                }else{
                    return heightArray[indexPath.row]
                }
            }else{
                return heightArray[indexPath.row]
            }
            
        }else{
            return RATIO_H(maxNum: 200)
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? MoreLoanTypeCell
        cell?.dataArray = dataArray[indexPath.row]
        cell?.title = titleArray[indexPath.row]
        cell?.delegate = self
        cell?.selectionStyle = .none
        
        if indexPath.row == 2 {
            cell?.selectIndex = self.selectIndex
            if self.selectIndex == 4 {//显示更多银行
                if moreBankView == nil{
                    moreBankView = MoreLoanBankView()
                    moreBankView.delegate = self
                    cell?.contentView.addSubview(moreBankView)
                    moreBankView.snp.makeConstraints { (make) in
                        make.left.right.equalToSuperview()
                    make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
                        make.height.equalTo(RATIO_H(maxNum: 280))
                    }
                }
            }else{//移除更多银行
                if moreBankView != nil {
                    moreBankView.removeFromSuperview()
                    moreBankView = nil
                }
            }
        
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

extension MoreLoanTypeView : MoreLoanTypeCellDelegate,MoreLoanBankViewDelegate {
    
    //MARK: 修改cell坐标
    func changeTableViewFrame(height: CGFloat) {
        
        heightArray.append(height + RATIO_H(maxNum:50))
        if heightArray.count == dataArray.count {
            self.isFlag = true
            self.tableView.reloadData()
        }
    }
    
    //MARK: 选择贷款详情
    func selectLoanDetails(title: String,text: String,index: Int) {
        
        self.delegate?.selectLoanDetails!(title: title, text: text)
        
        //选择银行
        if title == typeofInstitution {
            
            self.selectIndex = index
            
            //刷新指定cell
            let indexPath = NSIndexPath.init(row: 2, section: 0)
            self.tableView.reloadRows(at: [indexPath as IndexPath], with: .none)
            
        }
    }
    
    //MARK: 选择贷款银行
    func selectLoanBankType(title: String) {
        
        self.delegate?.selectLoanBankType!(title: title)
    }
    
}
